var e = require;
var t = module;
var o = exports;
var n;
Object.defineProperty(o, "__esModule", {value: !0});
var r = cc.gfx,
    s = cc._decorator,
    c = s.ccclass,
    l = s.property,
    d = s.executeInEditMode,
    u = s.requireComponent,
    p = [cc.v2(-50, -50), cc.v2(50, -50), cc.v2(50, 50), cc.v2(-50, 50)],
    h = (function (e) {
        function t() {
            var t = (null !== e && e.apply(this, arguments)) || this;
            return (
                (t._editing = !1),
                (t._offset = cc.v2(0, 0)),
                (t._spriteFrame = null),
                (t._vertexes = p),
                (t._meshCache = null),
                (t.renderer = null),
                (t.mesh = null),
                (t.texture = null),
                t
            );
        }
        return (
            __extends(t, e),
            Object.defineProperty(t.prototype, "editing", {
                get: function () {
                    return this._editing;
                },
                set: function (e) {
                    (this._editing = e), this._applyVertexes();
                },
                enumerable: !1,
                configurable: !0
            }),
            Object.defineProperty(t.prototype, "offset", {
                get: function () {
                    return this._offset;
                },
                set: function (e) {
                    (this._offset = e), this.editing && this._applyVertexes();
                },
                enumerable: !1,
                configurable: !0
            }),
            Object.defineProperty(t.prototype, "spriteFrame", {
                get: function () {
                    return this._spriteFrame;
                },
                set: function (e) {
                    this._spriteFrame = e;
                },
                enumerable: !1,
                configurable: !0
            }),
            Object.defineProperty(t.prototype, "vertexes", {
                get: function () {
                    return this._vertexes;
                },
                set: function (e) {
                    (this._vertexes = e), this._updateMesh(), this.editing && this._applyVertexes();
                },
                enumerable: !1,
                configurable: !0
            }),
            (t.prototype.start = function () {
                (this._meshCache = {}), this._updateMesh();
                var e = this.node.getComponent(cc.MeshRenderer);
                (e.mesh = null), (this.renderer = e);
                var t = cc.MaterialVariant.createWithBuiltin("unlit", this).material;
                e.setMaterial(0, t), this._applySpriteFrame(), this._applyVertexes();
            }),
            (t.prototype._updateMesh = function () {
                var e = this._meshCache[this.vertexes.length];
                e ||
                    ((e = new cc.Mesh()).init(
                        new r.VertexFormat([
                            {name: r.ATTR_POSITION, type: r.ATTR_TYPE_FLOAT32, num: 2},
                            {name: r.ATTR_UV0, type: r.ATTR_TYPE_FLOAT32, num: 2}
                        ]),
                        this.vertexes.length,
                        !0
                    ),
                    (this._meshCache[this.vertexes.length] = e)),
                    (this.mesh = e);
            }),
            (t.prototype._applyVertexes = function () {
                var e = this,
                    t = this.mesh,
                    o = this.vertexes.map(function (t) {
                        return t.add(e.offset);
                    });
                if ((t.setVertices(r.ATTR_POSITION, o), this.texture)) {
                    for (var n = [], i = 0, a = o; i < a.length; i++) {
                        var s = a[i],
                            c = (s.x + this.texture.width / 2) / this.texture.width,
                            l = 1 - (s.y + this.texture.height / 2) / this.texture.height;
                        n.push(cc.v2(c, l));
                    }
                    t.setVertices(r.ATTR_UV0, n);
                }
                if (this.vertexes.length >= 3) {
                    for (var d = [], u = [].concat(o), p = 0, h = -1; u.length > 3; ) {
                        var f = u[p % u.length],
                            _ = u[(p + 1) % u.length],
                            y = u[(p + 2) % u.length],
                            m = _.sub(f),
                            g = y.sub(_);
                        if (m.cross(g) >= 0) {
                            for (var v = !1, w = 0, b = u; w < b.length; w++) {
                                var C = b[w];
                                if (C !== f && C !== _ && C !== y && this._testInTriangle(C, f, _, y)) {
                                    v = !0;
                                    break;
                                }
                            }
                            if (v) {
                                if ((p = (p + 1) % u.length) === h) {
                                    cc.log("循环一圈未发现");
                                    break;
                                }
                            } else
                                (d = d.concat([o.indexOf(f), o.indexOf(_), o.indexOf(y)])),
                                    u.splice(u.indexOf(_), 1),
                                    (h = p);
                        } else if ((p = (p + 1) % u.length) === h) {
                            cc.log("循环一圈未发现");
                            break;
                        }
                    }
                    (d = d.concat(
                        u.map(function (e) {
                            return o.indexOf(e);
                        })
                    )),
                        t.setIndices(d),
                        this.renderer.mesh != t && (this.renderer.mesh = t);
                }
            }),
            (t.prototype._testInTriangle = function (e, t, o, n) {
                var i = o.sub(t),
                    a = n.sub(t),
                    r = n.sub(o),
                    s = e.sub(t),
                    c = e.sub(o);
                return (
                    i.cross(a) >= 0 != i.cross(s) < 0 &&
                    i.cross(a) >= 0 != a.cross(s) >= 0 &&
                    r.cross(i) > 0 != r.cross(c) >= 0
                );
            }),
            (t.prototype._applySpriteFrame = function () {
                if (this.spriteFrame) {
                    var e = this.renderer.getMaterial(0),
                        t = this.spriteFrame.getTexture();
                    e.define("USE_DIFFUSE_TEXTURE", !0), e.setProperty("diffuseTexture", t), (this.texture = t);
                }
            }),
            __decorate([l({serializable: !1, readonly: !0})], t.prototype, "_editing", void 0),
            __decorate([l], t.prototype, "editing", null),
            __decorate([l], t.prototype, "_offset", void 0),
            __decorate([l], t.prototype, "offset", null),
            __decorate([l({type: cc.SpriteFrame})], t.prototype, "_spriteFrame", void 0),
            __decorate([l({type: cc.SpriteFrame})], t.prototype, "spriteFrame", null),
            __decorate([l({type: cc.Vec2})], t.prototype, "_vertexes", void 0),
            __decorate([l({type: cc.Vec2})], t.prototype, "vertexes", null),
            __decorate([c, d, u(cc.MeshRenderer)], t)
        );
    })(cc.Component);
o.default = h;
